﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RightControl.Common {
    public static class DateTimeExt {

        public static string ToString(this DateTime d, DateFormatEnum dformat) {
            if (!(d == DateTime.MinValue)) {
                switch (dformat) {
                    case DateFormatEnum.月日年:
                        return d.ToString("M/d/yyyy", DateTimeFormatInfo.InvariantInfo);
                    case DateFormatEnum.月日:
                        return d.ToString("MM/dd", DateTimeFormatInfo.InvariantInfo);
                    case DateFormatEnum.小时分钟秒:
                        return d.ToString("HH:mm:ss", DateTimeFormatInfo.InvariantInfo);
                    case DateFormatEnum.小时分钟:
                        return d.ToString("HH:mm", DateTimeFormatInfo.InvariantInfo);
                    case DateFormatEnum.年月日小时分钟:
                        return d.ToString("yyyy/MM/dd HH:mm", DateTimeFormatInfo.InvariantInfo);
                    case DateFormatEnum.月日年小时分钟:
                        return d.ToString("M/d/yyyy HH:mm", DateTimeFormatInfo.InvariantInfo);
                    case DateFormatEnum.月日小时分钟:
                        return d.ToString("MM/dd HH:mm", DateTimeFormatInfo.InvariantInfo);
                    case DateFormatEnum.全部换行显示:
                        return d.ToString("yyyy/MM/dd<br>HH:mm:ss", DateTimeFormatInfo.InvariantInfo);
                    case DateFormatEnum.全部显示:
                        return d.ToString("yyyy/MM/dd HH:mm:ss", DateTimeFormatInfo.InvariantInfo);
                    //case DateFormatEnum.相对:
                    //    return Relative(d);
                    //case DateFormatEnum.相对年月日: {
                    //        string text4 = Relative(d);
                    //        return d.ToString("yyyy/MM/dd", DateTimeFormatInfo.InvariantInfo) + ((text4.Length == 0) ? "" : (" " + text4));
                    //    }
                    //case DateFormatEnum.相对年月日小时分钟: {
                    //        string text2 = Relative(d);
                    //        return d.ToString("yyyy/MM/dd HH:mm", DateTimeFormatInfo.InvariantInfo) + ((text2.Length == 0) ? "" : (" " + text2));
                    //    }
                    //case DateFormatEnum.相对月日小时分钟: {
                    //        string text6 = Relative(d);
                    //        return d.ToString("MM/dd HH:mm", DateTimeFormatInfo.InvariantInfo) + ((text6.Length == 0) ? "" : (" " + text6));
                    //    }
                    //case DateFormatEnum.相对全部: {
                    //        string text5 = Relative(d);
                    //        return d.ToString("yyyy/MM/dd HH:mm:ss", DateTimeFormatInfo.InvariantInfo) + ((text5.Length == 0) ? "" : (" " + text5));
                    //    }
                    case DateFormatEnum.间隔:
                        return Interval(d);
                    case DateFormatEnum.间隔年月日: {
                            string text3 = Interval(d);
                            return d.ToString("yyyy/MM/dd", DateTimeFormatInfo.InvariantInfo) + ((text3.Length == 0) ? "" : (" " + text3));
                        }
                    case DateFormatEnum.间隔全部: {
                            string text = Interval(d);
                            return d.ToString("yyyy/MM/dd HH:mm:ss", DateTimeFormatInfo.InvariantInfo) + ((text.Length == 0) ? "" : (" " + text));
                        }
                    case DateFormatEnum.中文日期:
                        return d.ToString("yyyy年MM月dd日", DateTimeFormatInfo.InvariantInfo);
                    default:
                        return d.ToString("yyyy/MM/dd", DateTimeFormatInfo.InvariantInfo);
                    case DateFormatEnum.亚马逊:
                        return d.ToString("yyyy-MM-ddTHH:mm:ssZ", DateTimeFormatInfo.InvariantInfo);
                    case DateFormatEnum.UTC:
                        return d.ToString("yyyy-MM-ddTHH:mm:ss", DateTimeFormatInfo.InvariantInfo);
                }
            }
            return "";

        }


        public static string Interval(DateTime d) {
            DateTime now = DateTime.Now;
            if (!(now <= d)) {
                TimeSpan span = now.Subtract(d);
                return Interval(span);
            }
            return "";
        }

        public static string Interval(TimeSpan span) {
            return Interval("间隔", span, "");
        }

        public static string Interval(string prefix, TimeSpan span, string tail = "") {
            if (span.Days <= 365) {
                if (span.Days <= 99) {
                    if (span.Days <= 0) {
                        if (span.Hours <= 0) {
                            if (span.Minutes <= 0) {
                                if (span.Seconds <= 0) {
                                    return "";
                                }
                                return prefix + span.Seconds + "秒" + tail;
                            }
                            return prefix + span.Minutes + "分钟" + tail;
                        }
                        return prefix + span.Hours + "小时" + tail;
                    }
                    return prefix + span.Days + "天" + tail;
                }
                return prefix + span.Days / 30 + "月" + tail;
            }
            return prefix + span.Days / 365 + "年" + tail;
        }

        /// <summary>
        /// 时间转为TimeStamp
        /// </summary>
        /// <param name="time"></param>
        /// <returns></returns>
        public static string ToTimeStamp(DateTime time) {
            try {
                DateTime value = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
                return ((long)DateTime.Parse(time.ToString()).Subtract(value).TotalMilliseconds).ToString();
            }
            catch (Exception) {
                return "";
            }
        }
    }
}
